import Vue from 'vue'
import App from './App.vue'
import router from './router'
// 导入element-ui
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import {postRequest} from "./utils/api";
import {getRequest} from "./utils/api";
import {putRequest} from "./utils/api";
import {deleteRequest} from "./utils/api";

import store from "./store/index";
import {initMenu} from "@/utils/menus";

import 'font-awesome/css/font-awesome.css';
import {downloadRequest} from "@/utils/download";

// 使用Element-ui的
Vue.use(ElementUI)
Vue.config.productionTip = false
// 插件形式使用请求
Vue.prototype.postRequest = postRequest;
Vue.prototype.getRequest = getRequest;
Vue.prototype.putRequest = putRequest;
Vue.prototype.deleteRequest = deleteRequest;


Vue.prototype.downloadReuqest = downloadRequest;
// 路由前置守卫
router.beforeEach((to,from,next)=>{
  if (window.sessionStorage.getItem("tokenStr")){
    initMenu(router,store);
    if (window.sessionStorage.getItem('user') == null){
      getRequest('/admin/info').then(resp=>{
        if (resp != null){
          // 存入用户信息
          window.sessionStorage.setItem('user',JSON.stringify(resp));
          store.commit('INIT_ADMINS',resp);
          next();
        }
      })
    }
    next(); // 没有执行next()就不会跳到路由
    // console.log(next);
  }else {
    // 判断路由
    if (to.path == '/'){
      next();
    }else {
      next('/?redirect='+to.path);
    }
  }
});
new Vue({
  router,
  store,  // 引入Vuex
  render: h => h(App)
}).$mount('#app')
